﻿using System.Security.Cryptography;
using AutoMapper.Execution;
using B.S.DataBase.MyProject.Domain.Tickettoticket;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.GetIcesandCommand;
using T.E.ERP.Read.API.Dtos.Dtos;
using T.E.ERP.Write.API.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.GetIcesandCommandHander
{
    /// <summary>
    /// 销售订单
    /// 物联网-智能MES生产管理系统-07-开票到票管理V1.1-20250206
    /// </summary>
    public class GetIencesandCommandHander : IRequestHandler<GetIencesandCommand, APIResult<ApiPaging<BillingderDto>>>
    {

        private readonly IBaseRepository<Billingder> _billingderRepository;
        
        private readonly IBaseRepository<OrdersSele> baseRepository;

        public GetIencesandCommandHander(IBaseRepository<Billingder> billingderRepository, IBaseRepository<OrdersSele> baseRepository)
        {
            _billingderRepository = billingderRepository;
            this.baseRepository = baseRepository;
        }

        public Task<APIResult<ApiPaging<BillingderDto>>> Handle(GetIencesandCommand request, CancellationToken cancellationToken)
        {
            APIResult<ApiPaging<BillingderDto>> result = new APIResult<ApiPaging<BillingderDto>>();

            var list = from a in _billingderRepository.GetAll().Result
                       join b in baseRepository.GetAll().Result
                        on a.BillingStae equals b.OrdStae
                       select new BillingderDto
                       {
                           Bid =a.Bid,
                           BillingStae =a.BillingStae,
                           Billingtopic =a.Billingtopic,
                           Billingtype = a.Billingtype,
                           BillingDate = a.BillingDate,
                           Billingsale =a.Billingsale,

                           BillTicket = a.BillTicket,
                           number =a.number,
                           remark = a.remark,

                           Orid =b.Orid,
                           OrdStae = b.OrdStae,
                           Ordstopic = b.Ordstopic,
                           OrdstopName = b.OrdstopName,
                           OrdstopTime = b.OrdstopTime,
                           Ordstopamount = b.Ordstopamount,
                           Theamount = b.Theamount,
                           Tinvoice = b.Tinvoice,
                           Ordremark = b.Ordremark,
                       };

            if (!string.IsNullOrEmpty(request.OrdStae))
            {
                list = list.Where(x => x.OrdStae.Contains(request.OrdStae));
       
            }

            if (!string.IsNullOrEmpty(request.Ordstopic))
            {
                list = list.Where(x => x.Ordstopic.Contains(request.Ordstopic));
            }

            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.Bid).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize);


            ApiPaging<BillingderDto> api = new ApiPaging<BillingderDto>();
            api.TotalCount = TotalCount;
            api.TotalPage = TotalPage;
            api.list =  list.ToList();
            result.data = api;

            return Task.FromResult(result);
        }
    }
}
